package com.primes.util;

import java.util.LinkedHashMap;
import java.util.Map;

public class ObjectCache {

	static LinkedHashMap<String, Object> cache = null;

	static {
		final int MAX_ENTRIES = 10;
		cache = new LinkedHashMap<String, Object>(MAX_ENTRIES + 1, .75F, true) {
			public boolean removeEldestEntry(Map.Entry eldest) {
				return size() > MAX_ENTRIES;
			}
		};
	}

	public static <T> T getObjectFromCache(Class className ) throws Exception{
		System.out.println("cache size ." + cache.size());
		if(cache.get(className)==null){
			Object obj=ObjectIO.read(className);
			cache.put(className.getName(), obj);
			return (T)obj;
		}else{
			return (T)cache.get(className);
		}
		
	}


}
